import { type MockMethod } from 'vite-plugin-mock'

export default [
  {
    url: '/dev-api/user/login',
    method: 'post',
    response: ({ body }: any) => {
      const { username, password } = body
      if (username === 'admin' && password === '123456') {
        return {
          code: 200,
          data: {
            token: 'admin-token',
          },
          message: '登录成功',
        }
      }
      return {
        code: 401,
        data: null,
        message: '用户名或密码错误',
      }
    },
  },
  {
    url: '/dev-api/user/info',
    method: 'get',
    response: (request: any) => {
      // 检查请求头中的 X-token
      const authResult = checkAuth(request)
      if (authResult) {
        return authResult
      }
      return {
        code: 200,
        data: {
          data: {
            username: 'Admin',
            avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
          },
        },
        message: '获取用户信息成功',
      }
    },
  },
  {
    url: '/api/user/logout',
    method: 'post',
    response: (request: any) => {
      // 检查请求头中的 X-token
      const authResult = checkAuth(request)
      if (authResult) {
        return authResult
      }

      return {
        code: 200,
        data: null,
        message: '退出成功',
      }
    },
  },
] as MockMethod[]

// 鉴权方法
const checkAuth = (request: any) => {
  const xToken = request.headers['x-token']
  if (xToken !== 'admin-token') {
    return {
      code: 401,
      data: null,
      message: '未授权访问，请先登录',
    }
  }
}
